package com.gift.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gift.auto.JSONAuthentication;
import com.gift.entity.GiLpc;
import com.gift.entity.GiUser;
import com.gift.entity.LpcType;
import com.gift.entity.LpcUser;
import com.gift.mapper.GiLpcMapper;
import com.gift.mapper.GiUserMapper;
import com.gift.mapper.LpcTypeMapper;
import com.gift.mapper.LpcUserMapper;
import com.gift.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gift
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/lpc-user")
public class LpcUserController extends JSONAuthentication {
    @Autowired
    LpcUserMapper lpcUserMapper;
    @Autowired
    GiLpcMapper giLpcMapper;
    @Autowired
    LpcTypeMapper lpcTypeMapper;
    @Autowired
    GiUserMapper giUserMapper;
    @ApiOperation(value = "根据用户id查询拥有的礼品册")
    @PostMapping("/findByUserId")
    public void findByUserId(HttpServletRequest request, HttpServletResponse response,@RequestParam("userId") int userId, @RequestParam("size") int size, @RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<LpcUser> shUserPage=new Page<>(current,size);
        Page<LpcUser> shUserPages=lpcUserMapper.selectPage(shUserPage,wrapper);
        List<LpcUser> records = shUserPages.getRecords();
        int total=lpcUserMapper.selectCount(wrapper);
        for (LpcUser a:records){
            GiLpc giLpc = giLpcMapper.selectById(a.getLpcId());
            giLpc.setLpcType(lpcTypeMapper.selectById(giLpc.getTypeId()));
            a.setGiLpc(giLpc);
        }
        if(records.size()>0){
            Result ok = Result.ok().data("lpc",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据用户id查询拥有的礼品册转赠礼品册")
    @PostMapping("/findByUserIdZz")
    public void findByUserIdZz(HttpServletRequest request, HttpServletResponse response,@RequestParam("fromUser") int userId,@RequestParam("toUserName") String  username,@RequestParam("lpcId") int lpcId)throws  Exception{
            QueryWrapper wrapper2=new QueryWrapper();
            wrapper2.eq("username",username);
        GiUser giUser = giUserMapper.selectOne(wrapper2);
        if(giUser!=null){
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.eq("user_id",userId);
            wrapper.eq("lpc_id",lpcId);
            LpcUser lpcUser = lpcUserMapper.selectOne(wrapper);
            if(lpcUser==null){
                Result error = Result.error().message("该礼品册不存在");
                this.WriteJSON(request, response, error);
            }else {
                GiLpc giLpc = giLpcMapper.selectById(lpcId);
                if (!giLpc.getStatus().equals("正常")) {
                    Result error = Result.error().message("礼品册已被使用");
                    this.WriteJSON(request, response, error);
                } else {
                    lpcUser.setUserId(giUser.getId());
                    if (lpcUserMapper.updateById(lpcUser) > 0) {
                        Result ok = Result.ok().message("赠送成功");

                        this.WriteJSON(request, response, ok);
                    } else {
                        Result error = Result.error().message("赠送失败");
                        this.WriteJSON(request, response, error);
                    }
                }
            }
        }else{
            Result error = Result.error().message("没有此用户");
            this.WriteJSON(request, response, error);
        }

    }


    }



